home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / asyam.zip / DETECT.C < prev    next >
C/C++ Source or Header  |  1993-06-24  |  3KB  |  95 lines

  1. #include <dos.h>
  2.  
  3. #include "async.h"
  4.  
  5. extern  int UART_ports[];
  6.  
  7. int     async_detect_uart(int comport)
  8. {
  9.     int     x;
  10.     int     base = UART_ports[comport];
  11.  
  12.     outp(base+3,0x1b);
  13.     if (inp(base+3)!=0x1b) {
  14.         return(UART_NONE);
  15.     }
  16.  
  17.     outp(base+3,0x3);
  18.     if (inp(base+3)!=0x3) {
  19.         return(UART_NONE);
  20.     }
  21.  
  22.     outp(base+7,0x55);
  23.     if (inp(base+7)!=0x55) {
  24.         return(UART_8250);
  25.     }
  26.  
  27.     outp(base+7,0xAA);
  28.     if (inp(base+7)!=0xAA) {
  29.         return(UART_16460);
  30.     }
  31.  
  32.     outp(base+2,1);
  33.     x=inp(base+2);
  34.     if ((x&0x80)==0) {
  35.         return(UART_16460);
  36.     }
  37.     if ((x&0x40)==0) {
  38.         return(UART_16550);
  39.     }
  40.  
  41.     outp(base+2,0x0);
  42.  
  43.     return(UART_16550A);
  44. }
  45.  
  46. int     async_detect_irq(int comport)
  47. {
  48.     int     mask, irq, imr, ier, lcr, mcr;
  49.     int     base = UART_ports[comport];
  50.  
  51.     asm cli
  52.  
  53.     lcr = inp(base+LCR);                // Read LCR
  54.     outp(base+LCR, ~0x80 & lcr);        // Clear DLAB
  55.  
  56.     ier = inp(base+IER);                // Read IER
  57.     outp(base+IER, 0x00);               // Disable all UART interrupts
  58.  
  59.     mcr = inp(base+MCR);                // Read MCR
  60.     outp(base+MCR, (~0x10 & mcr) | 0x0C); // Enable UART interrupt generation
  61.  
  62.     imr = inp(0x21);                    // Read the interrupt mask register
  63.     outp(0x20, 0x0A);                   // Prepare to read the IRR
  64.  
  65.     mask = 0xFC;                        // The mask for IRQ2-7
  66.     outp(base+IER, 0x02);               // Enable 'Transmitter Empty' interrupt
  67.  
  68.     mask &= inp(0x20);                  // Select risen interrupt request
  69.     outp(base+IER, 0x00);               // Disable 'Transmitter Empty' interrupt
  70.  
  71.     mask &= ~inp(0x20);                 // Select fallen interrupt request
  72.     outp(base+IER, 0x02);               // Enable 'Transmitter Empty' interrupt
  73.  
  74.     mask &= inp(0x20);                  // Select risen interrupt request
  75.     outp(0x21, ~mask);                  // Unmask only this interrupt(s)
  76.  
  77.     outp(0x20, 0x0C);                   // Enter the poll mode
  78.  
  79.     irq = inp(0x20);                    // Accept the high level interrupt
  80.     inp(base+LSR);                      // Read LSR to reset line status interrupt
  81.     inp(base);                          // Read RBR to reset data ready interrupt
  82.     inp(base+IIR);                      // Read IIR to reset transmitter empty interrupt
  83.     inp(base+MSR);                      // Read MSR to reset modem status interrupt
  84.  
  85.     outp(base+IER, ier);                // Restore Interrupt Enable Reg
  86.     outp(base+LCR, lcr);                // Restore Line Control Reg
  87.     outp(base+MCR, mcr);                // Restore Modem Control Reg
  88.     outp(0x20, 0x20);                   // End of interrupt mode
  89.     outp(0x21, imr);                    // Restore Interrupt Mask Reg
  90.  
  91.     asm sti
  92.  
  93.     return (irq & 0x80) ? irq & 0x07 : -1;
  94. }
  95.